#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more 
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership. 
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with 
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

using System;
using System.Collections.Generic;
using System.Text;

namespace IPSim.Data
{
    /// <summary>
    /// Class representing Mac Address.
    /// Mac address is a 48bit value.
    /// An example of a MAC-48 address would be "00-08-74-4C-7F-1D"
    /// Broadcast  address is "FF-FF-FF-FF-FF-FF"
    /// </summary>
    public class MacAddress
    {
        public const Int64 macBroadcast = Int32.MaxValue;

        public static Random m_randomizer = new Random();
        private Int64 m_Address;

        public Int64 Address
        {
            get { return m_Address; }
        }

        public override bool Equals(System.Object obj)
        {
            // If parameter cannot be cast to IPAddress return false:
            MacAddress macAddr = obj as MacAddress;
            if ((object)macAddr == null)
            {
                return false;
            }

            // Return true if the addresses match
            return m_Address == macAddr.Address;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public bool Equals(MacAddress macAddress)
        {
            // Return true if the addresses match:
            return m_Address == macAddress.Address;
        }

        public MacAddress()
        {
            m_Address = m_randomizer.Next(Int32.MaxValue);
        }
        public MacAddress(Int64 address)
        {
            m_Address = address;
        }
        /// <summary>
        /// Converts 48bit integer that represents Mac Address to friendly string in format FF:FF:FF:FF:FF:FF
        /// </summary>
        public override string ToString()
        {
            return String.Format("{0:X}:{1:X00}:{2:X00}:{3:X00}:{4:X00}:{5:X00}",((m_Address>>40)&0xff),((m_Address>>32)&0xff),((m_Address>>24)&0xff),((m_Address>>16)&0xff),((m_Address>>8)&0xff),(m_Address&0xff));
        }

        public readonly static MacAddress BroadcastAddress = new MacAddress(macBroadcast);
    }
}
